
let storage = window.localStorage;
if (!storage) {
	console.log('浏览器版本太低，不支持localStorage');
}
export function post_callback(url, msg, elementId, func, debug) {
  var xhr = new XMLHttpRequest();
  xhr.open('POST', url);
  xhr.onload = function() {
    if (xhr.status === 200) {
      if (debug) {
        console.log('[++++] url:', url, "res:", xhr.responseText);
      } else {
        console.log('[++++] url:', url);//, "res:", xhr.responseText);
      }
      if (xhr.responseText) {
        var result = xhr.responseText;
        if (func)
          func(elementId, result);
      } else {
        document.getElementById("notice").innerText =
          '[ERROR] no response url:' + url;
      }
    } else {
      console.error('request failure:', xhr.status);
      document.getElementById("notice").innerText =
        '[ERROR] request url:' + url;
    }
  };
  xhr.send(msg);
  console.log('[----] url:', url, '. msg:', msg);
}
export function post_sql_form_with_callback(
  url, statement, elementId, func) {
  const params = new URLSearchParams();
  params.append('query', statement);
  if (storage.accountId) {
    params.append('accountId', storage.accountId);
  } else {
    params.append('accountId', "default");
  }
  if (storage.token) {
    params.append('token', storage.token);
  } else {
    params.append('token', "default");
  }
  post_callback(url, params.toString(), elementId, func);
}
export function post_xml_callback(url, statement, elementId, func) {
  var div = document.createElement('div');
  var child = document.createElement('div');
  div.appendChild(child);
  child.setAttribute("sql", statement);
  var msg = div.outerHTML;
  post_callback(url, msg, elementId, func);
}
export function post_files_callback(url, filesId, elementId, func, debug) {
  var formData = new FormData();
  var fileInput = document.getElementById(filesId);
  formData.append('option', "filesUpload");
  formData.append('file', fileInput.files[0]);
  var requestStr = formData;
  post_callback(url, requestStr, elementId, func, debug);
}
